﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
using System.Globalization;

namespace WeatherProviders
{
    internal class Helper
    {
        public static IWebProxy GetProxy()
        {
            if (Home.Base.GeneralHelper.Proxy != null) return Home.Base.GeneralHelper.Proxy;
            IWebProxy proxy = WebRequest.GetSystemWebProxy();
            proxy.Credentials = CredentialCache.DefaultCredentials;
            return proxy;
        }

        #region GetRequest
        public static String GetRequest(Uri path, Encoding encoding, Int32 Timeout)
        {
            StreamReader sr = null; HttpWebRequest request = null; HttpWebResponse response = null;
            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create(path);
                request.Timeout = Timeout;
                request.Proxy = GetProxy();
                request.UserAgent = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 8.0) (compatible; MSIE 8.0; Windows NT 5.1;)";
                response = (HttpWebResponse)request.GetResponse();
                if (!request.HaveResponse) return string.Empty;
                sr = new StreamReader(response.GetResponseStream(), encoding);
                String ret = sr.ReadToEnd();
                //for debug
                string _url = request.Address.ToString();
                return ret;
            }
            catch { return String.Empty; }
            finally
            {
                if (response != null) { response.Close(); response = null; }
                if (sr != null) { sr.Close(); sr.Dispose(); sr = null; }
                request = null;
            }
        }
        #endregion

        #region TeFelt
        public static double TeFelt(Double Temperature, Double Humidity, Double WindSpeed)
        {
            //Kelvin
            double var_1 = 7.5 * Temperature / (237.7 + Temperature);
            double var_2 = Math.Pow(10.0, var_1);
            double var_3 = 6.112 * var_2 * Humidity / 100.0;
            double Temperature2 = Temperature + 5.0 / 9.0 * (var_3 - 10);
            if (Temperature2 < Temperature) Temperature2 = Temperature;
            double Temperature1 = WindSpeed <= 2.0 ? Temperature2 : 0.045 * ((5.2735 * Math.Sqrt(WindSpeed * 3.0) + 10.45) - 0.2778 * WindSpeed * 3.0) * (Temperature2 - 33.0) + 33.0;
            return Math.Round(Temperature1);
        }
        #endregion
        #region TempF
        public static double TempF(double TempC)
        {
            return Math.Round(1.8 * TempC + 32.0);
        }
        #endregion

        internal static bool IsDay(DateTime Date, decimal Lat, decimal Lon)
        {
            SunCalculator cal = new SunCalculator(Date, (double)Lat, (double)Lon);
            //bool isDay = DateTime.Now > sunrise && DateTime.Now < sunset;
            return Date > cal.DSunRise && Date < cal.DSunSet;
        }
    }
}
